package com.itcast.bulls.stock.user.config;

import com.itcast.bulls.stock.common.generator.GlobalIDGenerator;
import com.itcast.bulls.stock.common.utils.GlobalConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

@Configuration
public class GlobalSystemConfiguration {

    //snowflake的工作组中心ID
    @Value("${snowflake.workerId}")
    private Long workerId;

    //snowflake的数据中心ID
    @Value("${snowflake.datacenterId}")
    private Long datacenterId;

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;


    //定义全局ID生成器
    @Bean
    public GlobalIDGenerator globalIDGenerator(){
        if(null == workerId || null == datacenterId){
            return new GlobalIDGenerator();
        }
        return new GlobalIDGenerator(workerId,datacenterId);
    }


    //tokenStore的配置
    @Bean
    public TokenStore tokenStore(){
        RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
        tokenStore.setPrefix(GlobalConstants.OAUTH_PREFIX_KEY);
        return tokenStore;
    }
}
